app.controller('AdsModalCtrl', function ($scope, $modalInstance, toaster, param, ajax, fileUpload, cos) {

	$scope.dplist = [];

	$scope.submitting = false;
	$scope.param = param;
	if ($scope.param.data == null) {
		$scope.param.data = {};
	}

	if ($scope.param.type == "update") {
		$scope.flag = true;
	}

	$scope.loaddplist = function () {
		$scope.dplist = [
			{ id: 1, name: "订票" },
			{ id: 2, name: "城际约租" },
			//			{ id: 3, name: "跨城巴士" },
			//			{ id: 4, name: "小件快运" },
			//			{ id: 5, name: "度假旅游" }
		];

		//设置默认值
		$scope.dplist.selected = $scope.dplist[0];
		if ($scope.param.data.moduleId != null) {
			$scope.dplist.selected = $scope.dplist[$scope.param.data.moduleId - 1];
		}
	};

	$scope.loaddplist();

	$scope.ok = function () {
		var uploadUrl = cos.host() + cos.img() + "?attributeName=ads";
		if ($scope.param.type == "insert") {
			if ($scope.myForm.$valid) {
				if ($scope.fileToUpload != undefined) {
					$scope.param.data.moduleId = $scope.dplist.selected.id;
					fileUpload.upload($scope.fileToUpload, uploadUrl, null,
						function (result) {
							if (result.code == 200) {
								$scope.param.data.imageUrl = result.filePath;
								$modalInstance.close($scope.param);
							} else {
								toaster.pop('error', '错误', result.message);
							}
							$scope.uploading = false;
						}, function () {
							toaster.pop('error', '错误', '文件上传失败！');
							$scope.uploading = false;
						}
					);
				} else {
					toaster.pop('warning', '提示', '请上传一张图片！');
					$scope.submitting = false;
					return;
				}
			}
		} else if ($scope.param.type == "update") {
			if ($scope.myForm.$valid) {
				$scope.param.data.moduleId = $scope.dplist.selected.id;
				if ($scope.fileToUpload == undefined) {
					$modalInstance.close($scope.param);
				} else {
					fileUpload.upload($scope.fileToUpload, uploadUrl, null,
						function (result) {
							if (result.code == 200) {
								$scope.param.data.imageUrl = result.filePath;
								$modalInstance.close($scope.param);
							} else {
								toaster.pop('error', '错误', result.message);
							}
							$scope.uploading = false;
						}, function () {
							toaster.pop('error', '错误', '文件上传失败！');
							$scope.uploading = false;
						}
					);
				}
			}
		}
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
		$scope.submitting = false;
	};
});